package com.h2cloud.ClassLoader;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @Package: com.h2cloud.ClassLoader
 * @ClassName: MyClassLoaderParentFirst
 * @Author: h2cloud
 * @CreateTime: 2024/4/14 1:07 上午
 * @Description:
 */
public class MyClassLoaderParentFirst extends ClassLoader{

	private Map<String, String> classPathMap = new HashMap<>();

	public MyClassLoaderParentFirst() {
		classPathMap.put("com.h2cloud.ClassLoader.TestA", "/Users/h2cloud/javaproject/javaStudy/target/classes/com/h2cloud/ClassLoader/TestA.class");
		classPathMap.put("com.h2cloud.ClassLoader.TestB", "/Users/h2cloud/javaproject/javaStudy/target/classes/com/h2cloud/ClassLoader/TestB.class");
	}

	// 重写了 findClass 方法
	@Override
	public Class<?> findClass(String name) throws ClassNotFoundException {
		String classPath = classPathMap.get(name);
		File file = new File(classPath);
		if (!file.exists()) {
			throw new ClassNotFoundException();
		}
		byte[] classBytes = getClassData(file);
		if (classBytes == null || classBytes.length == 0) {
			throw new ClassNotFoundException();
		}
		return defineClass(classBytes, 0, classBytes.length);
	}

	private byte[] getClassData(File file) {
		try (InputStream ins = new FileInputStream(file); ByteArrayOutputStream baos = new
				ByteArrayOutputStream()) {
			byte[] buffer = new byte[4096];
			int bytesNumRead = 0;
			while ((bytesNumRead = ins.read(buffer)) != -1) {
				baos.write(buffer, 0, bytesNumRead);
			}
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new byte[] {};
	}
}

